<?php
require_once("header.php");
require_once("MakeAllowedQuestionsSelectionTable.php");

GetUserInfo($_SESSION["userid"], &$userinfo);

$cmd = $_REQUEST["cmd"];

if($cmd=="Cancel"){
	?>
		<center>
		<META HTTP-EQUIV="refresh" CONTENT="0; URL=index.php">
		<p><font size="+1">Cancelling...</font></p>
		<p><a href="index.php">continue</a></p>
		</center>
	<?php
	ReturnToIndex();
}else if($cmd=="Save Changes"){
	SaveChanges();
	?>
		<center>
		<META HTTP-EQUIV="refresh" CONTENT="2; URL=index.php">
		<p><font size="+1">Changes saved.</font></p>
		<p><a href="index.php">continue</a> (automatic in 2 seconds)</p>
		</center>
	<?php
}else{
	ShowEditForm();
}


require_once("footer.php");


//---------------------------
// ShowEditForm
//---------------------------
function ShowEditForm()
{
	$edit_allowed_questions = MakeAllowedQuestionsSelectionTable($_SESSION["userid"]);

	?>

	<center>
	<table border='0' width='600' cellpadding='20'>
		<tr>
			<td>
				<font size='+1'><i>
				Use this table to select the question pool used to generate tests
				for <?=$userinfo["first_name"]?>. If no boxes are checked, it will
				be treated that same as if <b>all</b> boxes are checked.
				</i></font>
			</td>
		</tr>
	</table>
	</center>

	<center>
	<form method='POST'>
	<?= $edit_allowed_questions ?>

	<table border='0' cellpadding='20'>
		<tr>
			<td>
				<input type='submit' name='cmd' value='Cancel'>
			</td>
			<td>
				<input type='submit' name='cmd' value='Save Changes'>
			</td>
		</tr>
	</table>
	</form>
	</center>
	
	<?php
}


//---------------------------
// SaveChanges
//---------------------------
function SaveChanges()
{
	global $userinfo;

	// Get a list of all questionid values
	$all_questions = GetAllQuestionIDs();

	// Loop over all questionids and get the "checked" status for each
	$allowed_questions = array();
	foreach($all_questions AS $questionid){
		$val = $_REQUEST["q$questionid"];
		if($val=="on")$allowed_questions[] = $questionid;
	}
	
	// Replace the allowed questionids for this user in database
	ReplaceAllowedQuestions($_SESSION["userid"], $allowed_questions);
}



